<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
import config from '@/config/config'
export default {
  name: 'App',
  data () {
    return {
      configData: {
        'debug': true,
        'beta': false,
        'appId': 'wx8a0cd86933b74fe7',
        'nonceStr': 'EY9SunQ0Su',
        'timestamp': 1578469655,
        'url': 'http://zhanye.xiangxiangxiao.cn',
        'signature': '644d31666a3275eebdb86e0467168ea15dbad574',
        'jsApiList': [
          'getLocation',
          'chooseWXPay',
          'onMenuShareAppMessage',
          'onMenuShareTimeline',
          'chooseWXPay',
          'openLocation',
          'scanQRCode'
        ]
      }
    }
  },
  created () {
    if (this.$isWxBrowser()) {
      this.wxInit()
    }
    this.$H5Init().then(res => {
      if (!res) {
        this.imInit()
      }
      localStorage.setItem('path', this.$route.path)
      this.$store.commit('SET_PATH', this.$route.path)
    })
  },
  methods: {
    wxInit () {
      this.$axios.post('/api/wechat/sdk_config', config.wx_config).then(config => {
        console.log('微信sdk配置：', config)
        this.$store.commit('SET_WX_CONFIG', config.data)
        this.$wxConfig(config.data)
      })

      this.$wxReady().then(() => {
        console.log('wx.ready')
        this.$store.commit('SET_WX_INIT', true)

        if (this.uinfo && this.uinfo.id && this.wxAuth) {
          // 上报openid到服务器
          this.$api('POST', '/api/v1/user/profile', {
            openid: this.wxAuth.openid
          }).then(res => {
            console.log('上报openId返回结果:', res)
          })
        }

        // 设置一次分享默认配置
        this.$setWxShareConfig({})

        // 微信获取定位
        this.$wxGetLocation().then((res) => {
          console.log('地理位置：', res)
          this.$axios.get('/api/baidu/getCityCode', {
            params: {
              latitude: res.latitude,
              longitude: res.longitude
            }
          }).then(res => {
            if (res.status === 200) {
              localStorage.setItem('cityCode', res.data.result.cityCode)
              localStorage.setItem('cityName', res.data.result.addressComponent.city)
              this.$store.commit('SET_CITYNAME', res.data.result.addressComponent.city)
              this.$store.commit('SET_CITYCODE', res.data.result.cityCode)
            }
          })
        })
      }).catch(error => {
        console.log(error)
      })
    },
    imInit () {
      this.$RC_init().then(() => {
        console.log('rong in success')
        this.$RC_setConnectionStatusListener(function (st) {
          console.log('setConnectionStatusListener:', st)
        })

        // 融云消息监听
        this.$RC_setOnReceiveMessageListener()

        this.connectRC()
      })
    },
    connectRC () {
      if (this.$store.state.RongToken) {
        this.$RC_connect(this.$store.state.RongToken).then(res => {
          console.log('融云连接成功：', res)
          // 获取会话列表
          this.getConversationList()
          this.getTotalUnreadCount()

          window.rongChat.subscribe(() => {
            this.getConversationList()
            this.getTotalUnreadCount()
          })
        })
      }
    },
    // 获取会话列表
    getConversationList () {
      this.$RC_getConversationList().then(res => {
        console.log('会话列表：', res)
        this.$store.commit('SET_IMHIS', res)
      })
    },
    // 获取总未读数
    getTotalUnreadCount () {
      this.$RC_getTotalUnreadCount().then(res => {
        console.log('未读数', res)
      })
    }
  }
}
</script>

<style lang="scss">
#app{
  height: 100%;
  .not{
    position: fixed;
    top: .88rem;
    left: 0;
    width: 100%;
  }
}
</style>
